package com.smartlbs.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smartlbs.baseframe.web.ResponseInfo;
import com.smartlbs.dto.UserOrderDto;
import com.smartlbs.entity.UserBasic;
import com.smartlbs.entity.UserOrder;
import com.smartlbs.service.IUserOrderService;


/**
 * 订单基本信息控制层实现类
 * 
 * 
 *
 */
@Controller
@RequestMapping("order/basic")
public class UserOrderController {
	
	@Autowired
	private IUserOrderService userOrderService;
	
	/**
	 * 下单
	 * 
	 * 
	 * 
	 * @param request
	 * @param code
	 * @return
	 */
	@RequestMapping("add")
	@ResponseBody
	public ResponseInfo add(HttpServletRequest request, UserOrder userOrder){
		//
		int result = 0;
		String message = "";
		
		UserBasic userBasic = (UserBasic)request.getAttribute("currentUser");
		
		try {
			int data = userOrderService.addUserOrder(userOrder, userBasic);
			if(data == 2){
				result = 2;
				message = "余额不足";
			}else if(data == 3){
				message = "赛事已结束,无法支付";
			}else{
				result = 1;
				message = "订单创建成功";
			}
		} catch (Exception e) {
			message = "订单创建失败";
			e.printStackTrace();
		}
		
		/*
		 * 返回值信息
		 */
		ResponseInfo responseInfo = new ResponseInfo();
		
		responseInfo.setResult(result);
		responseInfo.setMessage(message);
		
		return responseInfo;
	}
	
	/**
	 * 支付
	 * 
	 * 
	 * 
	 * @param request
	 * @param orderId
	 * @return
	 */
	@RequestMapping("pay")
	@ResponseBody
	public ResponseInfo payAccount(HttpServletRequest request, Long orderId, String points){
		int result = 0;
		String message = "";
		
		try {
			Integer ret = userOrderService.payOrder(orderId, points);
			
			if(ret == 1){
				message = "支付成功";
			}else if(ret == 2){
				message = "赛事已结束,无法支付";
			}else{
				message = "该订单不存在";
			}
			
			result = 1;
		} catch (Exception e) {
			message = "支付失败";
			e.printStackTrace();
		}
		
		/*
		 * 返回值信息
		 */
		ResponseInfo responseInfo = new ResponseInfo();
		
		responseInfo.setResult(result);
		responseInfo.setMessage(message);
		
		return responseInfo;
	}
	
	/**
	 * 删除订单
	 * 
	 * 
	 * 
	 * @param request
	 * @param orderId
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public ResponseInfo delete(HttpServletRequest request, Long orderId){
		//
		int result = 0;
		String message = "";
		
		try {
			userOrderService.delete(orderId);
			
			result = 1;
			message = "删除成功";
		} catch (Exception e) {
			message = "删除失败";
			e.printStackTrace();
		}
		
		/*
		 * 返回值信息
		 */
		ResponseInfo responseInfo = new ResponseInfo();
		
		responseInfo.setResult(result);
		responseInfo.setMessage(message);
		
		return responseInfo;
	}
	
	/**
	 * 结算
	 * 
	 * 
	 * 
	 * @param request
	 * @param code
	 * @return
	 */
	@RequestMapping("accountByBet")
	@ResponseBody
	public ResponseInfo accountByBet(HttpServletRequest request, String betIds){
		//
		int result = 0;
		String message = "";
		
		try {
			String[] ids = betIds.split(",");
			for (String betId : ids) {
				userOrderService.accountByBet(Long.valueOf(betId));
			}
			
			
			result = 1;
			message = "结算成功";
		} catch (Exception e) {
			message = "结算失败";
			e.printStackTrace();
		}
		
		/*
		 * 返回值信息
		 */
		ResponseInfo responseInfo = new ResponseInfo();
		
		responseInfo.setResult(result);
		responseInfo.setMessage(message);
		
		return responseInfo;
	}
	
	/**
	 * 获取订单信息
	 * 
	 * 
	 * 
	 * @param request
	 * @param code
	 * @return
	 */
	@RequestMapping("find")
	@ResponseBody
	public ResponseInfo find(HttpServletRequest request, Integer pageNo, Integer pageSize, UserOrderDto userOrderDto){
		//
		int result = 0;
		String message = "";
		Object data = null;
		//
		pageNo = pageNo == null?1:pageNo;
		pageSize = pageSize == null?10:pageSize;

		if(userOrderDto.getUserId() != null && userOrderDto.getUserId() == 0){
			//
			UserBasic userBasic = (UserBasic)request.getAttribute("currentUser");
			
			userOrderDto.setUserId(userBasic.getUserId());
		}
		
		try {
			data = userOrderService.find(pageNo, pageSize, userOrderDto);
			
			result = 1;
			message = "数据获取成功";
		} catch (Exception e) {
			message = "数据获取失败";
			e.printStackTrace();
		}
		
		/*
		 * 返回值信息
		 */
		ResponseInfo responseInfo = new ResponseInfo();
		
		responseInfo.setResult(result);
		responseInfo.setMessage(message);
		responseInfo.setData(data);
		
		return responseInfo;
	}
	
	/**
	 * 获取订单数量信息信息
	 * 
	 * 
	 * 
	 * @param request
	 * @param code
	 * @return
	 */
	@RequestMapping("queryTitle")
	@ResponseBody
	public ResponseInfo queryTitle(HttpServletRequest request, UserOrderDto userOrderDto){
		//
		int result = 0;
		String message = "";
		Object data = null;
		
		if(userOrderDto.getUserId() != null && userOrderDto.getUserId() == 0){
			//
			UserBasic userBasic = (UserBasic)request.getAttribute("currentUser");
			
			userOrderDto.setUserId(userBasic.getUserId());
		}
		
		try {
			data = userOrderService.queryTitle(userOrderDto);
			
			result = 1;
			message = "数据获取成功";
		} catch (Exception e) {
			message = "数据获取失败";
			e.printStackTrace();
		}
		
		/*
		 * 返回值信息
		 */
		ResponseInfo responseInfo = new ResponseInfo();
		
		responseInfo.setResult(result);
		responseInfo.setMessage(message);
		responseInfo.setData(data);
		
		return responseInfo;
	}
}